//## 검사기준정보 정보 저장 ####################
function fnSaveRule()
{
	if (!$("#"+"chk1a").val())
	{
		alert("검사 기준 최대값을 입력해 주세요.");
		$("#"+"chk1a").focus();
		return;
	}
	if (!$("#"+"chk1b").val())
	{
		alert("검사 기준 최소값을 입력해 주세요.");
		$("#"+"chk1b").focus();
		return;
	}

	var param  = "fullLotNo="+$("#"+"fullLotNo").val();
		param += "&lotSubNo="+$("#"+"lotSubNo").val();
		param += "&rank="+$("#"+"rank").val();
		param += "&type="+$("#"+"type").val();
		param += "&checkDt="+$("#"+"checkDt").val();

		for (k=1; k<=20; k++)
		{
			param += "&chk"+k+"a="+$("#chk"+k+"a").val();
			param += "&chk"+k+"b="+$("#chk"+k+"b").val();
			var chk = "";
			if ($("#show"+k).is(":checked") == true) chk = "Y";
			else chk = "N";
			if ($("#pin"+k).is(":checked") == true) chk += "P";
			else chk += "O";
			param += "&chk"+k+"="+chk;
		}

	$.ajax({
		url: '/action/checking/saveRule.asp',
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				fnReloaded();
				//grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 검사기준정보 Reset() #######################
function fnResetRule()
{
	for (k=1; k<=20; k++)
	{
		$("#chk"+k+"a").val("");
		$("#chk"+k+"b").val("");
	}
}

//## 검사 측정 정보 저장
function fnSave()
{
	
	if (!$("#"+"chk1a").val())
	{
		alert("검사 기준 최대값을 입력해 주세요.");
		$("#"+"chk1a").focus();
		return;
	}
	if (!$("#"+"chk1b").val())
	{
		alert("검사 기준 최소값을 입력해 주세요.");
		$("#"+"chk1b").focus();
		return;
	}
	if (!$("#"+"result1").val())
	{
		alert("검사 측정값을 입력해 주세요.");
		$("#"+"result1").focus();
		return;
	}

	var param  = "fullLotNo="+$("#"+"fullLotNo").val();
		param += "&lotSubNo="+$("#"+"lotSubNo").val();
		param += "&rank="+$("#"+"rank").val();
		param += "&type="+$("#"+"type").val();
		param += "&visual="+$("#"+"visual").val();
		param += "&flag="+$("#"+"flag").val();
		param += "&memo="+$("#"+"memo").val();
		param += "&priority="+$("#"+"priority").val();

		for (k=1; k<=20; k++)
		{
			var result = $("#result"+k).val();
			if (result == "y") result = "Y";
			if (result == "n") result = "N";

			if (result == "Y") result = "-11111";
			else if (result == "N") result = "-99999";
			param += "&result"+k+"="+result;
		}
	$.ajax({
		url: '/action/checking/saveResult.asp',
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 검사 측정 정보 Reset()
function fnReset()
{
	for (k=1; k<=20; k++)
	{
		$("#result"+k).val("");
	}
	$("#priority").val("");
	$("#visual").val("Y");
	$("#flag").val("Y");
	$("#memo").val("");
}

//## 검사기준정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").show();
	setNullRow("loadSpan","1");
	var valUrl = "/action/checking/list.asp";
	var param = "fullLotNo="+$("#"+"fullLotNo").val();
		param += "&lotSubNo="+$("#"+"lotSubNo").val();
		param += "&rank="+$("#"+"rank").val();
		param += "&type="+$("#"+"type").val();
//window.open(valUrl+'?xMode=debug&'+param);
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("CheckingResult").find("Results").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var lotNo			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("lotNo").text();
		var priority		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("priority").text();
		var result1			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result1").text();
		var result2			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result2").text();
		var result3			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result3").text();
		var result4			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result4").text();
		var result5			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result5").text();
		var result6			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result6").text();
		var result7			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result7").text();
		var result8			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result8").text();
		var result9			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result9").text();
		var result10		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result10").text();
		var result11		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result11").text();
		var result12		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result12").text();
		var result13		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result13").text();
		var result14		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result14").text();
		var result15		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result15").text();
		var result16		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result16").text();
		var result17		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result17").text();
		var result18		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result18").text();
		var result19		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result19").text();
		var result20		= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("result20").text();
		var flag			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("flag").text();
		var memo			= $(G_grid01Rslt).find("CheckingResult").find("Results").eq(j).find("memo").text();

		ji = j+1;

		if (result1=="0.000") result1 = "&nbsp;";
		else if (result1=="-11111.000") result1 = "PASS";
		else if (result1=="-99999.000") result1 = "PIN-Fail";
		if (result2=="0.000") result2 = "&nbsp;";
		else if (result2=="-11111.000") result2 = "PASS";
		else if (result2=="-99999.000") result2 = "PIN-Fail";
		if (result3=="0.000") result3 = "&nbsp;";
		else if (result3=="-11111.000") result3 = "PASS";
		else if (result3=="-99999.000") result3 = "PIN-Fail";
		if (result4=="0.000") result4 = "&nbsp;";
		else if (result4=="-11111.000") result4 = "PASS";
		else if (result4=="-99999.000") result4 = "PIN-Fail";
		if (result5=="0.000") result5 = "&nbsp;";
		else if (result5=="-11111.000") result5 = "PASS";
		else if (result5=="-99999.000") result5 = "PIN-Fail";
		if (result6=="0.000") result6 = "&nbsp;";
		else if (result6=="-11111.000") result6 = "PASS";
		else if (result6=="-99999.000") result6 = "PIN-Fail";
		if (result7=="0.000") result7 = "&nbsp;";
		else if (result7=="-11111.000") result7 = "PASS";
		else if (result7=="-99999.000") result7 = "PIN-Fail";
		if (result8=="0.000") result8 = "&nbsp;";
		else if (result8=="-11111.000") result8 = "PASS";
		else if (result8=="-99999.000") result8 = "PIN-Fail";
		if (result9=="0.000") result9 = "&nbsp;";
		else if (result9=="-11111.000") result9 = "PASS";
		else if (result9=="-99999.000") result9 = "PIN-Fail";
		if (result10=="0.000") result10 = "&nbsp;";
		else if (result10=="-11111.000") result10 = "PASS";
		else if (result10=="-99999.000") result10 = "PIN-Fail";
		if (result11=="0.000") result11 = "&nbsp;";
		else if (result11=="-11111.000") result11 = "PASS";
		else if (result11=="-99999.000") result11 = "PIN-Fail";
		if (result12=="0.000") result12 = "&nbsp;";
		else if (result12=="-11111.000") result12 = "PASS";
		else if (result12=="-99999.000") result12 = "PIN-Fail";
		if (result13=="0.000") result13 = "&nbsp;";
		else if (result13=="-11111.000") result13 = "PASS";
		else if (result13=="-99999.000") result13 = "PIN-Fail";
		if (result14=="0.000") result14 = "&nbsp;";
		else if (result14=="-11111.000") result14 = "PASS";
		else if (result14=="-99999.000") result14 = "PIN-Fail";
		if (result15=="0.000") result15 = "&nbsp;";
		else if (result15=="-11111.000") result15 = "PASS";
		else if (result15=="-99999.000") result15 = "PIN-Fail";
		if (result16=="0.000") result16 = "&nbsp;";
		else if (result16=="-11111.000") result16 = "PASS";
		else if (result16=="-99999.000") result16 = "PIN-Fail";
		if (result17=="0.000") result17 = "&nbsp;";
		else if (result17=="-11111.000") result17 = "PASS";
		else if (result17=="-99999.000") result17 = "PIN-Fail";
		if (result18=="0.000") result18 = "&nbsp;";
		else if (result18=="-11111.000") result18 = "PASS";
		else if (result18=="-99999.000") result18 = "PIN-Fail";
		if (result19=="0.000") result19 = "&nbsp;";
		else if (result19=="-11111.000") result19 = "PASS";
		else if (result19=="-99999.000") result19 = "PIN-Fail";
		if (result20=="0.000") result20 = "&nbsp;";
		else if (result20=="-11111.000") result20 = "PASS";
		else if (result20=="-99999.000") result20 = "PIN-Fail";
		
		if (flag == "N") var bgColor = "#F2F2F2";
		else var bgColor = "#FFFFFF";

		if (!memo) memo = "&nbsp;";

		grid01Array[0] = lotNo;
		grid01Array[1] = memo;
		grid01Array[2] = result1;
		grid01Array[3] = result2;
		grid01Array[4] = result3;
		grid01Array[5] = result4;
		grid01Array[6] = result5;
		grid01Array[7] = result6;
		grid01Array[8] = result7;
		grid01Array[9] = result8;
		grid01Array[10] = result9;
		grid01Array[11] = result10;
		grid01Array[12] = result11;
		grid01Array[13] = result12;
		grid01Array[14] = result13;
		grid01Array[15] = result14;
		grid01Array[16] = result15;
		grid01Array[17] = result16;
		grid01Array[18] = result17;
		grid01Array[19] = result18;
		grid01Array[20] = result19;
		grid01Array[21] = result20;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,,,,,", "fnSetResult('"+priority+"')", boldLine);
	}
}


//## 검사기준정보 상세정보 표시 ####################
function fnSetResult(priority)
{
	var valUrl = "/action/checking/view.asp";
	var param = "priority="+priority;
		param += "&fullLotNo="+$("#"+"fullLotNo").val();
		param += "&lotSubNo="+$("#"+"lotSubNo").val();
		param += "&rank="+$("#"+"rank").val();
		param += "&type="+$("#"+"type").val();

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();

			for (k=1; k<=20; k++)
			{
				var rslt = $(xml).find("result"+k).text();
				if (rslt == "0.000") rslt = "";
				if (rslt == "-11111.000") rslt = "Y";
				if (rslt == "-99999.000") rslt = "N";
				$("#result"+k).val(rslt);
			}
			$("#priority").val($(xml).find("priority").text());
			$("#visual").val($(xml).find("visual").text());
			$("#flag").val($(xml).find("flag").text());
			$("#memo").val($(xml).find("memo").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 페이지 새로고침
function fnReloaded()
{
	
	var	param = "fullLotNo="+$("#"+"fullLotNo").val();
		param += "&lotSubNo="+$("#"+"lotSubNo").val();
		param += "&rank="+$("#"+"rank").val();
		param += "&type="+$("#"+"type").val();

	location.href="checking.asp?"+param;
}

//## 기존 검사 기준 불러오기
function fnSetHisRule(val)
{
	var	param = "fullLotNo="+$("#"+"fullLotNo").val();
		param += "&lotSubNo="+$("#"+"lotSubNo").val();
		param += "&rank="+$("#"+"rank").val();
		param += "&type="+$("#"+"type").val();
		param += "&hisRule="+val;

	location.href="checking.asp?"+param;
}
////## 검색 Reset 기능
//function fnSearchReset()
//{
//	$("#findUserNm").val("");
//}